package com.hue.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hue.entity.EduSubject;
import com.hue.entity.excel.SubjectData;
import com.hue.entity.excel.TreeData;
import com.hue.listener.ReadExcelListener;
import com.hue.mapper.EduSubjectMapper;
import com.hue.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-06-12
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void upload(MultipartFile file) throws IOException {
        InputStream is = file.getInputStream();
        EasyExcel easyExcel = new EasyExcel();
        easyExcel.read(is, SubjectData.class, new ReadExcelListener(this)).sheet().doRead();
    }

    @Override
    public List<TreeData> findAll() {
        List<TreeData> treeDatas = new ArrayList<>();
        Map<String,TreeData> map = new HashMap<>();
        //查询所有课程分类
        List<EduSubject> eduSubjects = list(null);
        //将一级分类存储到map中
        for (EduSubject eduSubject : eduSubjects) {
            if ("0".equals(eduSubject.getParentId())) {   //一级分类
                TreeData treeData = new TreeData(); //创建树形节点
                treeData.setId(eduSubject.getId());
                treeData.setLabel(eduSubject.getTitle());
                //将节点添加到map中
                map.put(treeData.getId(),treeData);
            }
        }
        //再次遍历二级分类
        for (EduSubject eduSubject : eduSubjects) {
            if (!"0".equals(eduSubject.getParentId())) {   //二级分类
                TreeData treeData = new TreeData();
                treeData.setId(eduSubject.getId());
                treeData.setLabel(eduSubject.getTitle());
                //将二级分类添加到对应额一级分类节点下
                TreeData parentTreeData = map.get(eduSubject.getParentId());
//                parentTreeData.getChildren().add(treeData);
                parentTreeData.add(treeData);
            }
        }
        //将map中的数据赋值到list中
        for(String id:map.keySet()){
            treeDatas.add(map.get(id));
        }
        return treeDatas;
    }

    @Override
    public List<EduSubject> findAllOneSubject() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> eduSubjects = list(wrapper);
        return eduSubjects;
    }

    @Override
    public List<EduSubject> findAllTwoSubject(String parentId) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<EduSubject> eduSubjects = list(wrapper);
        return eduSubjects;
    }
}
